package com.rmdjzz.slmmn.controller.admin.adminManagement;

import com.rmdjzz.slmmn.pojo.Admin;
import com.rmdjzz.slmmn.pojo.base.Singer;
import com.rmdjzz.slmmn.pojo.vo.RecommendGroupVo;
import com.rmdjzz.slmmn.pojo.vo.TypeGroupVo;
import com.rmdjzz.slmmn.service.RecommendService;
import com.rmdjzz.slmmn.service.SingerService;
import com.rmdjzz.slmmn.service.SongService;
import com.rmdjzz.slmmn.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @作者 张绍鑫
 * @创建时间 2023/5/27
 */
@Controller
@Api(tags = "管理员管理")
@Slf4j
@RequestMapping("/admin")
public class AdminManagement {
    @Autowired
    private TypeService typeService;
    @Autowired
    private RecommendService recommendService;
    @Autowired
    private SongService songService;
    @Autowired
    private SingerService singerService;


    @GetMapping("/adminLogin")
    @ApiOperation("前往登录")
    public String adminLogin(){
        return "adminLogin";
    }

    @PostMapping("/doAdminLogin")
    @ApiOperation("登录校验")
    public String doAdminLogin(Admin admin, Model model, HttpSession session){
        if (admin!=null){
            if (admin.getAdminAccount().equals("admin")){
                if (admin.getPassword().equals("admin")){
                    session.setAttribute("admin",admin);

                    List<TypeGroupVo> typeGroupVos=typeService.typeGroup();
                    System.out.println(typeGroupVos);
                    for (TypeGroupVo typeGroupVo:typeGroupVos){
                        System.out.println(typeGroupVo);
                        System.out.println(typeService.getById(typeGroupVo.getTypeId()).getType());
                        typeGroupVo.setType(typeService.getById(typeGroupVo.getTypeId()).getType());
                    }
                    model.addAttribute("typeGroupVos",typeGroupVos);

                    int sum=songService.sum();
                    log.info("sum:"+sum);
                    model.addAttribute("sumNum",sum);


                    List<RecommendGroupVo> recommendGroupVos = typeService.recommendGroup();
                    for (RecommendGroupVo recommendGroupVo:recommendGroupVos){
                        log.info("recommendVo:"+recommendGroupVo);
                        recommendGroupVo.setRecommend(recommendService.getById(recommendGroupVo.getRecommendId()).getRecommend());
                    }
                    model.addAttribute("recommendGroupVos",recommendGroupVos);


                    return "indexAdmin";
                }
            }
        }
        return "adminLogin";
    }
    @GetMapping("/resetSoaring")
    @ResponseBody
    public int resetSoaring(){
        return songService.resetSoaring();
    }
    @GetMapping("/adminLogOut")
    @ResponseBody
    public void adminLogOut(HttpSession session,Model model){
        session.invalidate();
    }

}
